package com.tripleying.dogend.dnfipm.util;

import com.jfoenix.controls.JFXAlert;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialogLayout;
import com.jfoenix.controls.JFXTextField;
import com.tripleying.dogend.dnfipm.App;
import java.util.function.Consumer;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;

/**
 * 消息工具
 * @author Dogend
 */
public class MessageUtil {
    
    public static JFXAlert alertSimpleMessage(String title, String body, String btn){
        JFXAlert alert = new JFXAlert(App.getStage());
        alert.initModality(Modality.APPLICATION_MODAL);
        alert.setHideOnEscape(false);
        alert.setOverlayClose(false);
        JFXDialogLayout layout = new JFXDialogLayout();
        Label titleLabel = new Label(title);
        if(title.contains("成功")){
            titleLabel.setTextFill(Color.LIGHTGREEN);
        }else if(title.contains("错误")){
            titleLabel.setTextFill(Color.RED);
        }
        layout.setHeading(titleLabel);
        layout.setBody(new Label(body));
        layout.getStylesheets().add("styles/dialog.css");
        JFXButton closeButton = new JFXButton(btn);
        closeButton.getStyleClass().add("dialog-accept");
        closeButton.setOnAction((event) -> {
            alert.hideWithAnimation();
        });
        layout.setActions(closeButton);
        alert.setContent(layout);
        alert.show();
        return alert;
    }
    
    public static JFXAlert alertConfirmMessage(String title, String body, String close, JFXButton... confirm){
        JFXAlert alert = new JFXAlert(App.getStage());
        alert.initModality(Modality.APPLICATION_MODAL);
        alert.setHideOnEscape(false);
        alert.setOverlayClose(false);
        JFXDialogLayout layout = new JFXDialogLayout();
        Label titleLabel = new Label(title);
        if(title.contains("警告")){
            titleLabel.setTextFill(Color.RED);
        }
        layout.setHeading(titleLabel);
        layout.setBody(new Label(body));
        layout.getStylesheets().add("styles/dialog.css");
        JFXButton closeButton = new JFXButton(close);
        closeButton.getStyleClass().add("dialog-accept");
        closeButton.setOnAction((event) -> {
            alert.hideWithAnimation();
        });
        Node[] btns = new Node[confirm.length+1];
        for(int i=0;i<confirm.length;i++){
            btns[i] = confirm[i];
        }
        btns[btns.length-1] = closeButton;
        layout.setActions(btns);
        alert.setContent(layout);
        alert.show();
        return alert;
    }
    
    public static JFXAlert alertInputMessage(String title, String body, String placeholder, String confirm, String cancel, Consumer<String> fun){
        JFXAlert alert = new JFXAlert(App.getStage());
        alert.initModality(Modality.APPLICATION_MODAL);
        alert.setHideOnEscape(false);
        alert.setOverlayClose(false);
        JFXDialogLayout layout = new JFXDialogLayout();
        layout.setHeading(new Label(title));
        JFXTextField tf = new JFXTextField(placeholder);
        VBox vb = new VBox(new Label(body), tf);
        vb.setSpacing(5);
        layout.setBody(vb);
        layout.getStylesheets().add("styles/dialog.css");
        JFXButton confirmButton = new JFXButton(confirm);
        confirmButton.getStyleClass().add("dialog-accept");
        confirmButton.setOnAction((event) -> {
            fun.accept(tf.getText());
            alert.hideWithAnimation();
        });
        JFXButton cancelButton = new JFXButton(cancel);
        cancelButton.getStyleClass().add("dialog-accept");
        cancelButton.setOnAction((event) -> {
            alert.hideWithAnimation();
        });
        layout.setActions(new Node[]{confirmButton, cancelButton});
        alert.setContent(layout);
        alert.show();
        return alert;
    }
    
    public static Text createText(String name, String msg){
        Text txt = new Text(name+": "+msg+"\n");
        txt.setFont(new Font(12));
        switch(msg.substring(0, 1)){
            case "√":
                txt.setFill(Color.GREEN);
                break;
            case "×":
                txt.setFill(Color.RED);
                break;
            case "一":
                txt.setFill(Color.GRAY);
                break;
            default:
                txt.setFill(Color.CORNFLOWERBLUE);
                break;
        }
        return txt;
    }
    
    public static JFXButton createButton(String text, double font, double height, double width, Paint color, String css, EventHandler<ActionEvent> action){
        JFXButton btn = new JFXButton(text);
        btn.setFont(new Font(font));
        btn.setPrefHeight(height);
        btn.setPrefWidth(width);
        if(color!=null){
            btn.setTextFill(color);
        }
        if(css!=null){
            btn.getStyleClass().add(css);
        }
        if(action!=null){
            btn.setOnAction(action);
        }
        return btn;
    }
    
}
